<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 17/12/26
 * Time: 11:26
 */

class CourseModel
{

    public function priorListsAction($user,$page,$number=PAGE_NUMBER)
    {
        $data['lists'] = Db::name('live_list')
            ->field('ll.id as llid,ll.name,ll.date,left(ll.start_time,5) as start_time,left(ll.end_time,5) as end_time,ll.enrol,ud.nickname,ud.avatar,ifnull(e.status, 2) as booked')->alias('ll')
            ->join('users_detail ud','ll.udid = ud.id')
            ->join('enroll e','ll.id = e.follow AND e.udid = '.$user['id'],'LEFT')
            ->where(['ll.fid'=>$user['fid'],'ll.status'=>['in','1,3']])
            ->whereTime('date','>=','today')
            ->page($page,$number)
            ->order('ll.id desc')
            ->select();
        $data['page'] = $page;
        $data['count'] = Db::name('live_list')->where(['fid'=>$user['fid'],'status'=>['in','1,3']])->whereTime('date','>=','today')->count();
        return $data;
    }

    public function liveListsAction($user,$page,$number=PAGE_NUMBER)
    {
        $data['lists'] = Db::name('live_list')
            ->field('ll.id as llid,ll.name,ll.date,left(ll.start_time,5) as start_time,left(ll.end_time,5) as end_time,if(ll.password,1,0) as password,ll.click,ud.nickname,ud.avatar')->alias('ll')
            ->join('users_detail ud','ll.udid = ud.id')
            ->where(['ll.fid'=>$user['fid'],'ll.status'=>2])
            ->page($page,$number)
            ->order('ll.id desc')
            ->select();
        $data['page'] = $page;
        $data['count'] = Db::name('live_list')->where(['fid'=>$user['fid'],'status'=>2])->count();
        return $data;
    }

    public function backListsAction($user,$page,$number=PAGE_NUMBER)
    {
        $data['lists'] = Db::name('live_list')
            ->field('ll.id as llid,ll.name,left(ll.play_time,10) as play_time,left(ll.play,5) as play,if(ll.password,1,0) as password,ll.viewing,ud.nickname,ud.avatar')->alias('ll')
            ->join('users_detail ud','ll.udid = ud.id')
            ->where(['ll.fid'=>$user['fid'],'ll.status'=>9])
            ->page($page,$number)
            ->order('ll.play_time desc')
            ->select();
        $data['page'] = $page;
        $data['count'] = Db::name('live_list')->where(['fid'=>$user['fid'],'status'=>9])->count();
        return $data;
    }

    public function addWatchAction($llid,$user,$status)
    {
        if($status == 2){
            Db::name('live_list')->where(['id'=>$llid,'fid'=>$user['fid'],'status'=>2])->inc('click')->setInc('viewing');
        }
        if($status == 9){
            Db::name('live_list')->where(['id'=>$llid,'fid'=>$user['fid'],'status'=>9])->setInc('viewing');

            if(Db::name('watch')->where(['llid'=>$llid,'udid'=>$user['id'],'fid'=>$user['fid']])->count()){
                Db::name('watch')->where(['llid'=>$llid,'udid'=>$user['id'],'fid'=>$user['fid']])->setInc('hits');
                $msg['message'] = '已观看过此培训';
            }else{
                $data['llid'] = $llid;
                $data['udid'] = $user['id'];
                $data['fid'] = $user['fid'];
                $data['hits'] = 1;
                Db::name('watch')->insert($data);
                $msg['message'] = '已观看完此培训';
            }
            return $msg;
        }
    }

    public function playAction($user,$llid)
    {
        $data = Db::name('live_list')
            ->field('ll.id as llid,ll.name,ll.view,ll.share,ll.volume,ll.annex,ll.channel,ll.status,ll.video,ll.date,left(ll.start_time,5) as start_time,left(ll.end_time,5) as end_time,ll.click,ll.introduction,ud.nickname,ud.avatar')->alias('ll')
            ->join('users_detail ud','ll.udid = ud.id')
            ->where(['ll.id'=>$llid,'ll.fid'=>$user['fid'],'ll.status'=>2])
            ->order('ll.id desc')
            ->find();
        $play = Db::name('live_list')
            ->field('SEC_TO_TIME(TIMESTAMPDIFF(SECOND,play_time,NOW())) as play')
            ->where(['id'=>$llid,'fid'=>$user['fid']])
            ->find();
        $data['play'] = $play['play'];
        if($data['view']){
            $data['annex'] = json_decode($data['annex'],true);
        }else{
            $data['annex'] = [];
        }
        return $data;
    }

    public function liveAction($user)
    {
        $data = Db::name('live_list')
            ->field('ll.id as llid,ll.name,ll.view,ll.share,ll.volume,ll.annex,ll.channel,ll.status,ll.date,left(ll.start_time,5) as start_time,left(ll.end_time,5) as end_time,ll.click,ll.introduction,ud.nickname,ud.avatar')->alias('ll')
            ->join('users_detail ud','ll.udid = ud.id')
            ->where(['ll.fid'=>$user['fid'],'ll.password'=>0,'ll.status'=>2])
            ->order('ll.id desc')
            ->find();
        if($data) {
            $play = Db::name('live_list')
                ->field('SEC_TO_TIME(TIMESTAMPDIFF(SECOND,play_time,NOW())) as play')
                ->where(['id' => $data['llid'], 'fid' => $user['fid']])
                ->find();
            $data['play'] = $play['play'];
            if($data['view']){
                $data['annex'] = json_decode($data['annex'],true);
            }else{
                $data['annex'] = [];
            }
        }
        return $data;
    }

    public function backAction($user,$llid)
    {
        $data = Db::name('live_list')
            ->field('ll.id as llid,ll.name,ll.view,ll.share,ll.date,left(ll.start_time,5) as start_time,left(ll.end_time,5) as end_time,ll.volume,ll.introduction,ll.status,ll.channel,ll.viewing,ll.score,ll.annex,ll.image,ll.video,ud.nickname,ud.avatar')->alias('ll')
            ->join('users_detail ud','ll.udid = ud.id')
            ->where(['ll.id'=>$llid,'ll.fid'=>$user['fid'],'ll.status'=>9])
            ->order('ll.id desc')
            ->find();
        if($data['view']){
            $data['annex'] = json_decode($data['annex'],true);
        }else{
            $data['annex'] = [];
        }
        $data['rate'] = $this->rateAction($user['fid'],$llid);
        $data['watch'] = $this->watchListAction($user,['llid'=>$llid]);
        return $data;
    }

    public function watchListAction($user,$content)
    {
        $data = Db::name('watch')
            ->field('w.hits,ud.phone,ud.nickname,ud.section,ud.duties,ud.avatar')->alias('w')
            ->join('users_detail ud','w.udid = ud.id')
            ->where(['w.llid'=>$content['llid'],'w.fid'=>$user['fid']])
            ->select();
        return $data;
    }

    public function rateAction($fid,$llid)
    {
        $rate = Db::name('score')
            ->field('ifnull(FORMAT(sum(score)/count(udid),0), 100) as score')
            ->where(['fid'=>$fid,'llid'=>$llid,'status'=>1])
            ->group('llid')
            ->select();
        if(empty($rate)){
            $rate = 100;
        }else{
            $rate = $rate[0]['score'];
        }
        return $rate;
    }

    public function enrollAction($llid,$user)
    {
        $enroll = Db::name('enroll');
        $where['udid'] = $user['id'];
        $where['fid'] = $user['fid'];
        $where['follow'] = $llid;
        $enrollDb = $enroll->where($where)->find();
        $status = 0;
        if($enrollDb){
            $status = $enrollDb['status'];
        }
        switch ($status){
            case 1:
                $msg['message'] = '您已取消关注';
                $msg['status'] = '2';
                $enroll->where($where)->setField('status', 2);
                $liveList = Db::name('live_list');
                $liveList->where('id',$llid)->setDec('enrol');
                break;
            case 2:
                $msg['message'] = '关注成功';
                $msg['status'] = '1';
                $enroll->where($where)->setField('status', 1);
                $liveList = Db::name('live_list');
                $liveList->where('id',$llid)->setInc('enrol');
                break;
            default:
                $msg['message'] = '关注成功';
                $msg['status'] = '1';
                $where['status'] = 1;
                $where['firm'] = $user['firm'];
                $enroll->insert($where);
                $liveList = Db::name('live_list');
                $liveList->where('id',$llid)->setInc('enrol');
                break;
        }
        $liveListDb = $liveList->where('id',$llid)->find();
        $msg['llid'] = $llid;
        $msg['number'] = $liveListDb['enrol'];
        return $msg;
    }

    public function passwordAction($content,$user)
    {
        $data = Db::name('live_list')
            ->where(['id'=>$content['llid'],'password'=>$content['password'],'fid'=>$user['fid'],'status'=>['in','2,9']])
            ->find();
        return $data;
    }

    public function scoreAction($content,$user,$number=PAGE_NUMBER)
    {
        $data['myScore'] = Db::name('score')
            ->field('score,comment')
            ->where(['udid'=>$user['id'],'fid'=>$user['fid'],'llid'=>$content['llid'],'status'=>1])
            ->page($content['page'],$number)
            ->find();
        if(empty($data['myScore'])){
            $data['myScore'] =[];
        }
        $rate = Db::name('score')
            ->field('FORMAT(sum(score)/count(udid),0) as score')
            ->where(['fid'=>$user['fid'],'llid'=>$content['llid'],'status'=>1])
            ->group('llid')
            ->select();
        if(empty($rate)){
            $data['rate'] = 100;
        }else{
            $data['rate'] = $rate[0]['score'];
        }
        $data['lists'] = Db::name('score')
            ->field('s.score,s.comment,s.createtime,ud.nickname,ud.avatar,ud.section')->alias('s')
            ->join('users_detail ud','s.udid = ud.id')
            ->where(['s.fid'=>$user['fid'],'s.llid'=>$content['llid'],'s.status'=>1])
            ->page($content['page'],$number)
            ->order('s.id desc')
            ->select();
        $data['page'] = $content['page'];
        $data['count'] = Db::name('score')->where(['fid'=>$user['fid'],'llid'=>$content['llid'],'status'=>1])->count();
        return $data;
    }

    public function addScoreAction($content,$user)
    {
        $data['udid'] = $user['id'];
        $data['fid'] = $user['fid'];
        $data['llid'] = $content['llid'];
        $score = Db::name('score');
        if($score->where($data)->find()){
            $score->where($data)->update(['comment'=>$content['comment'],'score'=>$content['score'],'status'=>1]);
        }else{
            $data['status'] = 1;
            $data['firm'] = $user['firm'];
            $data['score'] = $content['score'];
            $data['comment'] = $content['comment'];
            $score->insert($data);
        }
        $msg['message'] = '评分成功';
        return $msg;
    }

    public function delScoreAction($content,$user)
    {
        $data['udid'] = $user['id'];
        $data['fid'] = $user['fid'];
        $data['llid'] = $content['llid'];
        $data['status'] = 1;
        Db::name('score')->where($data)->update(['status'=>0]);
        $msg['message'] = '删除成功';
        return $msg;
    }


    public function giftListsAction()
    {
        $where['parent'] = 20;
        $where['type'] = 1;
        $where['enabled'] = 1;
        $data = Db::name('config')->field('id as cid,name,description as image')->where($where)->order('sort asc')->select();
        return $data;
    }

    public function giftSendAction($content,$user)
    {
        $live = Db::name('live_list')->field('id as cid,udid,status')->where('id',$content['llid'])->find();
        if($live['status'] != 2){
            $msg['message'] = '直播已结束';
            $msg['code'] = 400;
        }else{
            $data['udid'] = $user['id'];
            $data['firm'] = $user['firm'];
            $data['fid'] = $user['fid'];
            $data['llid'] = $content['llid'];
            $data['cid'] = $content['cid'];
            $data['receive'] = $live['udid'];
            $data['description'] = '礼物赠送';
            Db::name('prizes')->insert($data);
            $configDb = Db::name('config')->field('amount')->where('id',$content['cid'])->find();
            Db::name('live_list')->where('id',$content['llid'])->setInc('score',$configDb['amount']);
            $msg['message'] = '已向主播送出礼物';
        }
        return $msg;
    }

    public function giftRecordAction($user,$page,$number=PAGE_NUMBER)
    {
        $data['lists'] = Db::name('prizes')
            ->field('p.llid,p.receive,ud.nickname,ud.avatar,ll.name,count(p.llid) as number')->alias('p')
            ->join('users_detail ud','p.receive = ud.id')
            ->join('live_list ll','p.cid = ll.id')
            ->where(['p.udid'=>$user['id'],'p.fid'=>$user['fid']])
            ->page($page,$number)
            ->group('p.llid')
            ->order('p.id desc')
            ->select();
        $data['page'] = $page;
        $data['count'] = Db::name('prizes')
            ->where(['udid'=>$user['id'],'fid'=>$user['fid']])
            ->group('llid')
            ->count();
        return $data;
    }
}